<?php


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TacheManager
 *
 * @author camara
 */
require_once '../model/Tache.php';
require_once 'TypeTacheManager.php';

class TacheManager {
    
    private $bdd;
    
    function __construct($bdd) {
        $this->bdd = $bdd;
    }
    
     public function creerTache(stdClass $tache,$id,$cat){
         
         // Créer la tâche 
         $today= \date('Y-m-d');
         $rappel = \date("Y-m-d", strtotime($tache->rappel));
         $echeance = \date("Y-m-d", strtotime($tache->echeance));
         $req = $this->bdd->prepare('INSERT INTO TACHES (idType, dateCreation,dateRappel,dateEcheance,libelle,commentaire) VALUES(:type, :crea ,:rap,:ech,:lib,:com)');
         $req->execute(array('type' => $tache->type,'crea' =>$today,'rap' => $rappel,'ech' => $echeance,'lib' => $tache->libelle,'com' => $tache->comment));
         
         // Lier l'utilisateur au type de la tâche demandé 
         if( $cat == 1){
             $req2 = $this->bdd->prepare('INSERT INTO TACHE_PROJET (idUs,idProj,idTac) VALUES (:iduser,:idprojet,:idtache)');
         }else if ( $cat == 2){
             $req2 = $this->bdd->prepare('INSERT INTO TACHE_EXAMEN (iduser,idExam,idTache) VALUES (:iduser,:idprojet,:idtache)');
         }else if ( $cat == 3){
             $req2 = $this->bdd->prepare('INSERT INTO TACHE_STAGE (idUs,idStage,idTac) VALUES (:iduser,:idprojet,:idtache)');
         }
         
         $req2->execute(array('iduser' => $id,'idprojet' =>$tache->idprojet,'idtache' => $this->bdd->lastInsertId()));
         
     }
     
     public function getListTache($id,$idUser,$cat){
         
        // lister des tâches d'un projet/examen (id)  d'un utilisateur    
        $listtache = array();
        if ( $cat == 1){
            $req = $this->bdd->prepare("SELECT * FROM TACHES JOIN TACHE_PROJET ON TACHES.idTache=TACHE_PROJET.idTac WHERE TACHE_PROJET.idUs=$idUser AND TACHE_PROJET.idProj=$id ORDER BY dateCreation DESC");
        }else if ( $cat == 2) {
            $req = $this->bdd->prepare("SELECT * FROM TACHES JOIN TACHE_EXAMEN ON TACHES.idTache=TACHE_EXAMEN.idTache WHERE TACHE_EXAMEN.iduser=$idUser AND TACHE_EXAMEN.idExam=$id ORDER BY dateCreation DESC");
        }else if ( $cat == 3) {
            $req = $this->bdd->prepare("SELECT * FROM TACHES JOIN TACHE_STAGE ON TACHES.idTache=TACHE_STAGE.idTac WHERE TACHE_STAGE.idUs=$idUser AND TACHE_STAGE.idStage=$id ORDER BY dateCreation DESC");
        }
        $req->execute();
        while ($donnees = $req->fetch()){
            $manager = new TypeTacheManager($this->bdd);
            $tache = new Tache($donnees["idTache"],$donnees["idType"],$manager->getLibelletypebyId($donnees["idType"]),$donnees["libelle"],$donnees["dateEcheance"],$donnees["dateRappel"],$donnees["commentaire"],$id);
            array_push($listtache, $tache);
        }
        return $listtache;
         
     }
     
     public function getTachebyId($id){
         
            $req = $this->bdd->prepare('SELECT * FROM TACHES  WHERE idTache = ?');
            $req->execute(array($id));
            while ($donnees = $req->fetch()){
                $manager = new TypeTacheManager($this->bdd);
                $tache = new Tache($donnees["idTache"],$donnees["idType"],$manager->getLibelletypebyId($donnees["idType"]),$donnees["libelle"],$donnees["dateEcheance"],$donnees["dateRappel"],$donnees["commentaire"],0);
            }
            return $tache;
     }
     
     public function defaultTache($cat){
            if ( $cat == 1){
               $tache = new Tache(0,1,"Presentations","","","","","");
            }else if ( $cat == 2) {
               $tache = new Tache(0,5,"Revisions","","","","","");
            }else if ( $cat == 3) {
               $tache = new Tache(0,3,"Entretien","","","","","");
            }
            return $tache;
     }
     
     public function UpdateTache(stdClass $tache){
         
        $rappel = \date("Y-m-d", strtotime($tache->rappel));
        $echeance = \date("Y-m-d", strtotime($tache->echeance));
        $req = $this->bdd->prepare('UPDATE TACHES SET idType=:type ,dateRappel=:rap,dateEcheance=:ech,libelle=:lib,commentaire=:com WHERE idTache ='.$tache->id.'');
        $req->execute(array('type' => $tache->type,'rap' => $rappel,'ech' => $echeance,'lib' => $tache->libelle,'com' => $tache->comment));
            
     }
     
     public function deleTache($id ,$cat){
         // Supprimer la tâche et la relation TACHE_PROJET grâce à la contrainte CASCADE
         $this->bdd->exec("DELETE FROM TACHES WHERE idTache =$id"); 
         
         // S'il s'agit de TACHE_EXAMEN requêter et supprimer 
         if ( $cat == 2) {
            $this->bdd->exec("DELETE FROM TACHE_EXAMEN WHERE idTache =$id");
         }else if ( $cat == 1 ){
            $this->bdd->exec("DELETE FROM TACHE_PROJET WHERE idTac =$id");
         }else if ( $cat == 3 ){
            $this->bdd->exec("DELETE FROM TACHE_STAGE WHERE idTac =$id");
         }
        
     }
     
     
     public function getListRappel($idUser){
         
         // La liste des tâches dont le rappel c'est Aujourd'hui
         $listtache = array();$today= \date('Y-m-d');
         
         // les tâches du projet          
         $req = $this->bdd->prepare("SELECT * FROM TACHES JOIN TACHE_PROJET ON TACHES.idTache=TACHE_PROJET.idTac WHERE dateRappel='$today' AND TACHE_PROJET.idUs=$idUser ");$req->execute();
         while ($donnees = $req->fetch()){
            $manager = new TypeTacheManager($this->bdd);
            $tache = new Tache($donnees["idTache"],$donnees["idType"],$manager->getLibelletypebyId($donnees["idType"]),$donnees["libelle"],$donnees["dateEcheance"],$donnees["dateRappel"],$donnees["commentaire"],$donnees["idProj"]);
            array_push($listtache, $tache);
         }
        
         // les tâches d'exam
         $req = $this->bdd->prepare("SELECT * FROM TACHES JOIN TACHE_EXAMEN ON TACHES.idTache=TACHE_EXAMEN.idTache WHERE dateRappel='$today' AND TACHE_EXAMEN.iduser=$idUser ");$req->execute();
         while ($donnees = $req->fetch()){
            $manager = new TypeTacheManager($this->bdd);
            $tache = new Tache($donnees["idTache"],$donnees["idType"],$manager->getLibelletypebyId($donnees["idType"]),$donnees["libelle"],$donnees["dateEcheance"],$donnees["dateRappel"],$donnees["commentaire"],$donnees["idExam"]);
            array_push($listtache, $tache);
         }
         
         // les tâches du stage 
         $req = $this->bdd->prepare("SELECT * FROM TACHES JOIN TACHE_STAGE ON TACHES.idTache=TACHE_STAGE.idTac WHERE dateRappel='$today' AND TACHE_STAGE.idUs=$idUser ");$req->execute();
         while ($donnees = $req->fetch()){
            $manager = new TypeTacheManager($this->bdd);
            $tache = new Tache($donnees["idTache"],$donnees["idType"],$manager->getLibelletypebyId($donnees["idType"]),$donnees["libelle"],$donnees["dateEcheance"],$donnees["dateRappel"],$donnees["commentaire"],$donnees["idStage"]);
            array_push($listtache, $tache);
         }
         
         return $listtache;
     }
     
    
}
